package com.yunkaweilai.android.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.github.lazylibrary.util.DensityUtil;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.login.LoginActivity;
import com.yunkaweilai.android.adapter.GuideViewAdapter;
import com.yunkaweilai.android.adapter.HeaderBannerAdapter;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.net.Glide.GlideUtils;
import com.yunkaweilai.android.utils.StatusBarUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 引导页
 * Created by Administrator on 2017/7/26.
 */

public class GuideActivity extends BaseActivity {


    @BindView(R.id.vp_banner)
    ViewPager vpBanner;
    @BindView(R.id.ll_index_container)
    LinearLayout llIndexContainer;
    @BindView(R.id.rl_banner)
    RelativeLayout rlBanner;
    @BindView(R.id.id_btn_next)
    Button idBtnNext;
    private ArrayList<ImageView> ivList = new ArrayList<>();
    private int[] imglist = new int[]{R.mipmap.ic_guild_1, R.mipmap.ic_guild_2, R.mipmap.ic_guild_4};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        StatusBarUtil.setStatusBarTranslucent(this, false);
        View decor = this.getWindow().getDecorView();
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);//状态栏文字变白（强制变白状态）
        ButterKnife.bind(this);
        createImageViews();
        GuideViewAdapter adapter = new GuideViewAdapter(ivList);
        vpBanner.setAdapter(adapter);
        addIndicatorImageViews();
        setViewPagerChangeListener();
    }

    // 为ViewPager设置监听器
    private void setViewPagerChangeListener() {
        vpBanner.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                if (ivList == null || ivList.size() == 0) return;

                for (int i = 0; i < imglist.length; i++) {
                    llIndexContainer.getChildAt(i).setEnabled(i == position);
                }
                // 3.当滑动到最后一个添加按钮点击进入，
                if (position == imglist.length - 1) {
                    idBtnNext.setVisibility(View.VISIBLE);
                } else {
                    idBtnNext.setVisibility(View.GONE);
                }
            }
            @Override
            public void onPageScrolled(int position, float arg1, int arg2) {
            }
                @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    // 添加指示图标
    private void addIndicatorImageViews() {
        llIndexContainer.removeAllViews();
        for (int i = 0; i < 3; i++) {
            ImageView iv = new ImageView(mAc);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(DensityUtil.dip2px(mAc, 10), DensityUtil.dip2px(mAc, 10));
            lp.leftMargin = DensityUtil.dip2px(mAc, (i == 0) ? 0 : 7);
            iv.setLayoutParams(lp);
            iv.setBackgroundResource(R.drawable.xml_round_orange_grey_sel);
            iv.setEnabled(i == 0);
            llIndexContainer.addView(iv);
        }
    }

    // 创建要显示的ImageView
    private void createImageViews() {
        for (int i = 0; i < imglist.length; i++) {
            ImageView imageView = new ImageView(mAc);
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            imageView.setLayoutParams(params);
            GlideUtils.loadGlideImage(mAc, imglist[i], imageView);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            ivList.add(imageView);
        }
    }

    @OnClick(R.id.id_btn_next)
    public void onViewClicked() {
        intent2Activity(LoginActivity.class);
        finish();
    }
}
